package pl.koszolko.documentclustering.cluster.document;

import java.util.Set;

import pl.koszolko.documentclustering.dao.Document;

public class DocumentClusteringByCitedBy extends DocumentForClustering {

	private static final long serialVersionUID = 1L;

	public DocumentClusteringByCitedBy(Document doc) {
		super(doc);
	}
	
	@Override
	public double countSimilarity(DocumentForClustering document) {
		if(document instanceof DocumentClusteringByCitedBy) {
			return countSimilarityUsingCitedBy((DocumentClusteringByCitedBy)document);
		}
		throw new RuntimeException("Niepoprawny typ DocumentForClustering");
	}

	private double countSimilarityUsingCitedBy(
			DocumentClusteringByCitedBy document) {
		double allCitedBy = 0;
		double commonCitedBy = 0;
		
		allCitedBy = getCitedBySize();
		
		for(Document docRef : document.getCitedBy()) {
			if(ifCitedByContains(docRef)) {
				++commonCitedBy;
			} else {
				++allCitedBy;
			}
		}
		
		if(allCitedBy == 0)
			return 0D;
		else
			return commonCitedBy/allCitedBy;
	}
}
